package com.yanqu.road.server.timer.unionwar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.unionwar.UnionQunyingTimeInfo;
import com.yanqu.road.entity.activity.unionwar.cross.CrossUnionWarDetail;
import com.yanqu.road.entity.activity.unionwar.cross.CrossUnionWarState;
import com.yanqu.road.entity.activity.unionwar.enums.eTimeInfoPeriodType;
import com.yanqu.road.entity.activity.unionwar.result.CurrentTimeInfo;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarActivityMgr;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarGuessMgr;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarMgr;
import com.yanqu.road.utils.executor.GeneralTask;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CalcUnionWarGuessAchieveCountTask extends GeneralTask {

    private static final Logger logger = LogManager.getLogger(CalcUnionWarGuessAchieveCountTask.class.getName());

    private boolean isRun = false;
    private Map<Integer, List<Integer>> isCalcFirstStageOverMap = new HashMap<>();   //第一阶段竞猜奖励各档位达成人数是否统计完成
    private Map<Integer, List<Integer>> isCalcSecondStageOverMap = new HashMap<>();  //第二阶段竞猜奖励各档位达成人数是否统计完成
    @Override
    public void doRun() {
        if (!isRun) {
            isRun = true;
            Map<Integer, ActivityInfo> activityInfoMap = CrossUnionWarActivityMgr.getOpenActivityMap();
            for (ActivityInfo activityInfo : activityInfoMap.values()) {
                if(!isCalcFirstStageOverMap.containsKey(activityInfo.getActivityId())){
                    isCalcFirstStageOverMap.put(activityInfo.getActivityId(), new ArrayList<>());
                }
                if(!isCalcSecondStageOverMap.containsKey(activityInfo.getActivityId())){
                    isCalcSecondStageOverMap.put(activityInfo.getActivityId(), new ArrayList<>());
                }
                int phase = 0;
                CurrentTimeInfo currentTimeInfo = CrossUnionWarActivityMgr.getCurrentTimeInfo(activityInfo.getActivityId());
                UnionQunyingTimeInfo timeInfo = currentTimeInfo.getTimeInfo();
                if (timeInfo != null) {
                    phase = timeInfo.getType();
                } else if (currentTimeInfo.getPeriodType() == eTimeInfoPeriodType.ReceiveTime) {
                    phase = 2;
                }
                List<Integer> groupIdList = CrossUnionWarMgr.getGroupListByCrossServerId(activityInfo.getActivityId(), CrossBaseServer.getInstance().getServerId());
                for (int groupId : groupIdList) {
                    //统计第一阶段竞猜奖励各档位达成人数 积分赛所有阶段结束就可以进行
                    if (phase == 2 && !isCalcFirstStageOverMap.get(activityInfo.getActivityId()).contains(groupId)) {
                        //检测积分赛最后一场是否已开战
                        CrossUnionWarState crossUnionWarState = CrossUnionWarMgr.getCrossUnionWarState(activityInfo.getActivityId(), 1, CrossUnionWarActivityMgr.getAllSession(activityInfo.getActivityId(), 1), groupId);
                        if (crossUnionWarState.getState() != 2) {
                            continue;
                        }
                        //检测积分赛最后一场是否都已积分
                        List<CrossUnionWarDetail> lastList = CrossUnionWarMgr.getCrossUnionWarDetailList(activityInfo.getActivityId(), 1, CrossUnionWarActivityMgr.getAllSession(activityInfo.getActivityId(), 1), groupId);
                        int size = lastList.size();
                        if (size == 0) {
                            continue;
                        }
                        for (CrossUnionWarDetail detail : lastList) {
                            if (!detail.isAddScore()) {
                                size--;
                            }
                        }
                        if (size < lastList.size()) {
                            continue;
                        }
                        CrossUnionWarGuessMgr.calcConditionAchieveCount(activityInfo.getActivityId(), 1, groupId);
                        isCalcFirstStageOverMap.get(activityInfo.getActivityId()).add(groupId);
                    }
                }
                for (int groupId : groupIdList) {
                    //统计第二阶段竞猜奖励各档位达成人数 活动期结束就可以进行
                    if (currentTimeInfo.getPeriodType() == eTimeInfoPeriodType.ReceiveTime
                            && !isCalcSecondStageOverMap.get(activityInfo.getActivityId()).contains(groupId)) {
                        //检测决赛赛最后一场是否已开战
                        CrossUnionWarState crossUnionWarState = CrossUnionWarMgr.getCrossUnionWarState(activityInfo.getActivityId(), 2,
                                CrossUnionWarActivityMgr.getAllSession(activityInfo.getActivityId(), 2), groupId);
                        if (crossUnionWarState.getState() != 2) {
                            continue;
                        }
                        //检测决赛最后一场是否都已积分
                        List<CrossUnionWarDetail> lastList = CrossUnionWarMgr.getCrossUnionWarDetailList(activityInfo.getActivityId(), 2,
                                CrossUnionWarActivityMgr.getAllSession(activityInfo.getActivityId(), 2), groupId);
                        int addScoreNum = 0;
                        for (CrossUnionWarDetail detail : lastList) {
                            if (detail.isAddScore()) {
                                addScoreNum++;
                            }
                        }
                        if (addScoreNum < 2) {
                            continue;
                        }
                        CrossUnionWarGuessMgr.calcConditionAchieveCount(activityInfo.getActivityId(), 2, groupId);
                        isCalcSecondStageOverMap.get(activityInfo.getActivityId()).add(groupId);
                    }
                }
            }
            isRun = false;
        }

    }
}
